﻿#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QPixmap>
#include <QStringList>
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    i = 0;

    connect(ui->btn_open, &QPushButton::clicked, this, &Widget::getOpenFilenames);
    connect(ui->btn_previous, &QPushButton::clicked, this, &Widget::openPrevious);
    connect(ui->btn_next, &QPushButton::clicked, this, &Widget::openNext);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::getOpenFilenames()
{
    files = QFileDialog::getOpenFileNames(
                this,
                "Select one or more files to open",
                "./test_source",
                "Images (*.png *.xpm *.jpg)");

    qDebug() << "图片个数为：" << files.size();

    qDebug() << "第一张图片的序号为：" << i;

    QPixmap pixmap(files.at(i));
    ui->label_picture->setPixmap(pixmap);
}
void Widget::openPrevious()
{
    i--;
    i = (i +files.size()) % files.size();

    qDebug() << "previous picture number:" <<  i;

    QPixmap pixmap(files.at(i));
    ui->label_picture->setPixmap(pixmap);

}
void Widget::openNext()
{
    i++;
    i = (i + files.size()) % files.size();

    qDebug() << "next picture number:" <<  i;

    QPixmap pixmap(files.at(i));
    ui->label_picture->setPixmap(pixmap);

}
